<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\Withdrawal\AuditBatchAction;
use App\Admin\Renderable\DetailRenderable;
use App\Models\Order;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Admin;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Route;

class WithdrawalController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid(): Grid
    {
        //Admin::js('/js/withdrawal.js');

        return Grid::make(new Order(), function (Grid $grid) {
            $grid->model()->with('user')->with('payment_third_pay')
                ->where('type', Order::WITHDRAWAL)
                ->orderByDesc('id');

            $grid->column('id');
            $grid->column('user.tiktokno', trans('user.fields.tiktokno'))->display(function ($var){
                return $this->user_id."({$var})";
            });
            $grid->column('no', trans('order.fields.no'));
            $grid->column('payment_third_pay.title', '通道');
            $grid->column('payment_no', trans('order.fields.payment_no'));
            $grid->column('amount', trans('order.fields.amount'))->display(function ($var) {
                return $var / 100;
            });
            $grid->column('actual_amount', trans('order.fields.actual_amount'))->display(function ($var) {
                return $var / 100;
            });
            $grid->column('number', trans('withdrawal.fields.number'))->display(function () {
                if ($this->state == Order::SUCCESS) {
                    return Order::where('state', Order::SUCCESS)->where('type', Order::WITHDRAWAL)->where('id', '<=', $this->id)->where('user_id', $this->user_id)->count();
                }
                return '';
            });


            $grid->column('state', trans('order.fields.state'))->display(function (){
                // <i class="fa fa-circle" style="font-size: 13px;color: #dda451"></i>&nbsp;&nbsp;审核中
                if($this->payment_third_pay_id == 0 && $this->state == Order::SUCCESS){
                    $sucess_color = '#ce04e7';
                    $sucess_text = ' 手动成功';
                }else{
                    $sucess_color = '#28a745';
                    $sucess_text = ' 成功';
                }
                $value = [
                    Order::REVIEW => '审核中',
                    Order::PENDING => '进行中',
                    Order::SUCCESS => $sucess_text,
                    Order::FAILED => '失败',
                    Order::CANCEL => '取消',
                    Order::REFUSE => '拒绝',
                ];
                $color = [
                    Order::REVIEW => '#dda451',
                    Order::PENDING => '#17a2b8',
                    Order::SUCCESS => $sucess_color,
                    Order::FAILED => '#dc3545',
                    Order::CANCEL => '#6c757d',
                    Order::REFUSE => '#007bff',
                ];

                return '<i class="fa fa-circle" style="font-size: 13px;color: '.$color[$this->state].'"></i>&nbsp;&nbsp;'.$value[$this->state];
            });

            $grid->display("详细")->expand(function (Grid\Displayers\Expand $expand) {
                $str = "";
                $card = json_decode($this->card,true);
                $str .= "<div style='padding: 0 10px 0 10px;'>收款卡：" . $card['number'] ."</div>";
                if($card['bank_id'] == 1){
                    $str .= "<div style='padding: 0 10px 0 10px;'>收款银行：" . $card['ifsc'] ."-TRC</div>";
                }elseif ($card['bank_id'] == 2){
                    $str .= "<div style='padding: 0 10px 0 10px;'>收款银行：" . $card['ifsc'] ."-ERC</div>";
                }else{
                    $str .= "<div style='padding: 0 10px 0 10px;'>收款银行：" . $card['ifsc'] . "</div>";
                    $str .= "<div style='padding: 0 10px 0 10px;'>姓名：" . $card['username'] . "</div>";
                }

                if(!empty($this->response)){
                    $str .= "<div style='padding: 0 10px 0 10px;'><br />回调信息：" . $this->remark. '<Br>' . $this->response . '<Br>' .$this->notify_response . "</div>";
                }
                return $str;
            });
            //$grid->column('response', trans('order.fields.response'))->width('200px');
            //$grid->column('notify_response', trans('order.fields.notify_response'))->width('200px');

            $grid->column('created_at');
            $grid->column('updated_at');

            $grid->quickSearch(['user_id','user.tiktokno', 'no'])->placeholder(trans('order.fields.no') . '/' . trans('user.fields.tiktokno'));
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->like('user.id', trans('user.fields.id'));
                $filter->like('user.tiktokno', trans('user.fields.tiktokno'));
                //$filter->like('user.address', trans('user.fields.address'));
                $filter->equal('state', trans('order.fields.state'))->select(Order::STATE_MAP);
                $filter->between('created_at')->datetime();
                $filter->like('no', trans('order.fields.no'));
                $filter->like('payment_no', trans('order.fields.payment_no'));
            });

            $grid->footer(function ($collection) use ($grid) {
                $query = \App\Models\Order::query();
                // 拿到表格筛选 where 条件数组进行遍历
                $grid->model()->getQueries()->unique()->each(function ($value) use (&$query) {
                    if (in_array($value['method'], ['paginate', 'get', 'orderBy', 'orderByDesc'], true)) {
                        return;
                    }
                    $query = call_user_func_array([$query, $value['method']], $value['arguments'] ?? []);
                });
                // 查出统计数据
                $data = $query->where('state','=',\App\Models\Order::SUCCESS)->where('type', '=', \App\Models\Order::WITHDRAWAL)->sum('amount') / 100;
                $htmlstr = '<div class="d-flex" style="font-size: 16px;">总提现：<strong>'.$data.'</strong></div>';
                return $htmlstr;
            });


            $grid->disableCreateButton();
            $grid->disableActions();
            $grid->batchActions(function (Grid\Tools\BatchActions $actions) {
                $actions->add(new AuditBatchAction());
                $actions->disableDelete();
            });
            $grid->export();
        });
    }

}
